/**
 * 提示弹出框
 * @param title
 * @param content
 */
function simpleDialog(title, content){
	$("#myModal_h3").html(title);
	$("#myModal_p").html(content);
	$("#myModal").modal();
}

/**
 * 页面跳转（跳转到添加、修改页面时用）
 * @param url
 * @param data json格式数据{}
 */
function skipPage(url,data){
	$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	data = eval('(' + data + ')'); 
	$("#content").load(url,data, function(){
		$('#loading').remove();
		$('#content').fadeIn();
	});
}

/**
 * 表单数据提交及页面跳转
 * @param formId
 */
function formSubmit(formId){
	$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	var url = $("#"+formId).attr('action');
	$("#content").load(url,$("#"+formId).serializeArray(), function(){
		$('#loading').remove();
		$('#content').fadeIn();
	});
}


/**
 * 分页链接HTML
 * @param divId
 * @param formId
 * @param totalRow
 * @param pageSize
 * @param pageNum
 * @param totalPages
 * @param isSelectPage
 * @param isSelectSize
 * @param orderColunm
 * @param orderMode
 * @returns {String}
 */
function splitPageHtml(divId, formId, totalRow, pageSize, pageNum, totalPages, isSelectPage, isSelectSize, orderColunm, orderMode){
	var splitStr = '<ul class="pagination pagination-centered">';
	
	if (pageNum == 1 || totalPages == 0) {
		splitStr += '<li><a href="javascript:void(0)">上一页 </a></li>';
	} else {
		splitStr += '<li><a href="javascript:splitPageLink(\''+divId+'\', \''+formId+'\', ' + (pageNum - 1) + ');">上一页 </a></li>';
	}
	
	for (var i = 1; i <= totalPages; i++) {
        if (i == 2 && pageNum - 4 > 1) {
        	splitStr += '<li><a href="javascript:void(0)">...</a></li>';
            i = pageNum - 4;
        } else if (i == pageNum + 4 && pageNum + 4 < totalPages) {
        	splitStr += '<li><a href="javascript:void(0)">...</a></li>';
            i = totalPages - 1;
        } else {
            if (pageNum == i) {
            	splitStr += '<li class="active"><a href="javascript:void(0)" style="color: #272727; font-size: 14px; text-decoration: none;">' + pageNum + '</a></li>';
            } else {
            	splitStr += '<li><a href="javascript:splitPageLink(\''+divId+'\', \''+formId+'\', ' + i + ');" style="color: #898989; font-size: 14px;">';
            	splitStr += i;
            	splitStr += '</a></li>';
            }
        }
    }
	
	if (pageNum == totalPages || totalPages == 0) {
		splitStr += '<li><a href="javascript:void(0)">下一页 </a></li>';
	} else {
		splitStr += '<li><a href="javascript:splitPageLink(\''+divId+'\', \''+formId+'\', ' + (pageNum + 1) + ');">下一页 </a></li>';
	}
	
	if(isSelectPage == true){
		splitStr += '&nbsp;&nbsp;<li><select name="pageNum" onChange="splitPageLink(\''+divId+'\', \''+formId+'\', this.value);" style="width: 110px; height:35px;">';
		for (var i = 1; i <= totalPages; i++) {
			if (i == pageNum) {
				splitStr += '<option selected value="' + i + '"> 跳转到第 ' + i +'页</option>';
			} else {
				splitStr += '<option value="' + i + '"> 跳转到第 ' + i +'页</option>';
			}
		}
		if(totalPages == 0){
			splitStr += '<option value="0">无跳转数据 </option>';
		}
		splitStr += '</select>';
		splitStr += '<li>&nbsp;&nbsp;';
	}else{
		splitStr += '<input type="hidden" name="pageNum">';
	}
	
	if(isSelectSize == true){
		splitStr += '<li><select name="pageSize" onChange="splitPageLink(\''+divId+'\', \''+formId+'\', 1);" style="width: 90px; height:35px;">';
		
		var optionStr = '<option value="10">每页10条</option>';
		optionStr += '<option value="20">每页20条</option>';
		optionStr += '<option value="40">每页40条</option>';
		optionStr += '<option value="80">每页80条</option>';
		optionStr += '<option value="100">每页100条</option>';
		optionStr = optionStr.replace('"' + pageSize + '"', '"' + pageSize + '" selected="selected"');
		
		splitStr += optionStr;
		
		splitStr += '</select></li>';
	}else{
		splitStr += '<input type="hidden" name="pageSize">';
	}
	
	splitStr += '&nbsp;&nbsp;<li>共<strong>' + totalRow + '</strong>条记录</li>';
	
	splitStr += '</ul>';

	splitStr += '<input type="hidden" name="orderColunm" value="'+orderColunm+'"/>';
	splitStr += '<input type="hidden" name="orderMode" value="'+orderMode+'"/>';
	
	return splitStr;
}

/**
 * 分页链接处理
 * @param divId
 * @param formId
 * @param toPage
 */
function splitPageLink(divId, formId, toPage){
	$("#" + formId + " select[name=pageNum],input[name=pageNum] ").val(toPage);
	$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	var url = $("#"+formId).attr('action');
	$("#content").load(url,$("#"+formId).serializeArray(), function(){
		$('#loading').remove();
		$('#content').fadeIn();
	});
}

/**
 * 搜索
 * @param divId
 * @param formId
 */
function queryList(divId, formId){
	$("#" + formId + " select[name=pageNum],input[name=pageNum] ").val(1);
	$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	var url = $("#"+formId).attr('action');
	$("#content").load(url,$("#"+formId).serializeArray(), function(){
		$('#loading').remove();
		$('#content').fadeIn();
	});
}

/**
 * 分页列排序点击事件处理
 * @param divId
 * @param formId
 * @param colunmName
 */
function orderbyFun(thId,divId, formId, colunmName){
	var orderColunmNode = $("#" + formId + " input[name=orderColunm]");
	var orderColunm = orderColunmNode.val();
	
	var orderModeNode = $("#" + formId + " input[name=orderMode]");
	var orderMode = orderModeNode.val();
	
	if(colunmName == orderColunm){
		if(orderMode == ""){
			orderModeNode.val("asc");
		}else if(orderMode == "asc"){
			orderModeNode.val("desc");
		}else if(orderMode == "desc"){
			orderModeNode.val("");
		}
	}else{
		orderColunmNode.val(colunmName);
		orderModeNode.val("asc");
	}
	//alert(orderColunmNode.val()+"--"+orderModeNode.val());
	//ajaxForm(divId, formId);
	queryList(divId, formId);
}



/**
 * 弹出div显示简单效果，如查看用户详情
 * ajax请求url替换指定div
 * @param shade 是否开启遮罩层
 * @param divId 返回替换div
 * @param url 请求地址
 * @param data 参数
 * @param callback 回调
 */
function ajaxDiv(shade, divId, url, data, callback){
	if(data!=''){
		data = eval('(' + data + ')');
	}
	
	if(shade){
		$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	}
	
	$.ajax({
		type : "post",
		url : encodeURI(encodeURI(url)),
		data : data,
		dataType : "json",
		async: false,
		cache: false,
		success:function(returnData){
			if(returnData["statusCode"]==301){
				alert("网页失效，请重新登录！");
				if(returnData["type"]=="mgt")
					window.location.href=wcsurl+"mgt/sys/toLogin.do";
				else
					window.location.href=wcsurl+"ph/sys/toLogin.do";
			}else{
				//扩展回调函数
				if( callback != null ){
					callback(returnData,divId);
				}
			}
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) { 
			alert("请求出现错误！");
        },
        complete: function(XMLHttpRequest, textStatus) { 
        	if(shade){
        		$('#loading').remove();
    			$('#content').fadeIn();
        	}
        }
	});
}





function ajaxForm(shade, divId, url, formId, callback){
	if(shade){
		$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	}
	
	$.ajax({
		type : "post",
		url : encodeURI(encodeURI(url)),
		data : $("#"+formId).serializeArray(),
		dataType : "json",
		async: false,
		cache: false,
		success:function(returnData){
			if(returnData["statusCode"]==301){
				alert("网页失效，请重新登录！");
				if(returnData["type"]=="mgt")
					window.location.href=wcsurl+"mgt/sys/toLogin.do";
				else
					window.location.href=wcsurl+"ph/sys/toLogin.do";
			}else{
				//扩展回调函数
				if( callback != null ){
					callback(returnData,divId);
				}
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) { 
			alert("请求出现错误！");
        },
        complete: function(XMLHttpRequest, textStatus) { 
        	if(shade){
        		$('#loading').remove();
    			$('#content').fadeIn();
        	}
        }
	});
}


/**
 * 公共ajax函数
 * @param url
 * @param data
 * @param callback
 * @returns {String}
 */
function ajaxFunc(url, data, callback){
	var result = "";
	$.ajax({
		type : "post",
		url : encodeURI(encodeURI(cxt + url)),
		data : data,
		dataType : "html",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		async: false,
		cache: false,
		success:function(response){
			result = response;
			//扩展回调函数
			if( callback != null ){
				callback();
			}
		}
	});
	return result;
}

/**
 * ajax提交form替换content
 * @param divId 返回替换div
 * @param formId 提交formid
 * @param callback 回调
 */
function ajaxForm2(divId, formId, callback){
	$('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
	$("#" + formId).ajaxSubmit({
		cache: false,
	    success:  function (data) {
	    	if(data != ""){
	    		$("#" + divId).html(data);
	    	}
			//扩展回调函数
			if( callback != null ){
				callback();
			}
	    	$('#loading').remove();
			$('#content').fadeIn();
			docReady();
	    }
	});
}



